package com.xms.ribbonbutton.api.controller;

import com.xms.core.exception.XmsException;
import com.xms.core.model.KeyValuePair;
import com.xms.ribbonbutton.api.model.CreateRibbonButtonModel;
import com.xms.ribbonbutton.core.RibbonButtonArea;
import com.xms.ribbonbutton.entity.RibbonButton;
import com.xms.ribbonbutton.service.IRibbonButtonCreaterService;
import com.xms.utils.UUIDUtil;
import com.xms.web.framework.controller.ApiBaseController;
import com.xms.web.framework.model.JsonResultObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.UUID;

/**
 * <p>
 * RibbonButtonCreaterController
 * </p>
 *
 * @author migo
 * @since 2021/9/17
 */
@RestController
@RequestMapping("{org}/api/ribbonbutton")
public class RibbonButtonCreaterController extends ApiBaseController {
    private IRibbonButtonCreaterService _ribbonButtonCreaterService;
    @Autowired
    public void RibbonButtonCreaterController(IRibbonButtonCreaterService ribbonButtonCreaterService) {
        _ribbonButtonCreaterService = ribbonButtonCreaterService;
    }

    @ApiOperation("新建按钮")
    @PostMapping("create")
    public JsonResultObject create(@RequestBody CreateRibbonButtonModel model) throws XmsException, SQLException {
        RibbonButton entity = new RibbonButton();
        UUID id = UUIDUtil.get();
        entity.setRibbonbuttonid(id);
        entity.setCommandrules(model.commandrules);
        entity.setShowlabel(model.showlabel);
        entity.setShowarea(model.showarea);
        entity.setJslibrary(model.jslibrary);
        entity.setJsaction(model.jsaction);
        entity.setDisplayorder(model.displayorder);
        entity.setCssclass(model.cssclass);
        entity.setLabel(model.label);
        entity.setShowlabel(model.showlabel);
        entity.setIcon(model.icon);
        entity.setDescription(model.description);
        entity.setEntityid(model.entityid);
        entity.setCreatedby(_currentUser.getUser().SystemUserId);
        entity.setStatecode(true);
        entity.setAuthorizationenabled(false);
        entity.setCreatedon(LocalDateTime.now());
        if (entity.getShowarea() != RibbonButtonArea.Form && entity.getShowarea() != RibbonButtonArea.ListRow)
        {
            entity.setCommandrules("");
        }
        _ribbonButtonCreaterService.create(entity);
        return JCreateSuccess(new KeyValuePair<>("id", id));
    }
}
